package tuvienna.jade.participant;

import jade.core.AID;

import java.math.BigDecimal;

import tuvienna.jade.InterestSet;

public class EasilyBoredParticipant extends LazyParticipantAgent {

	protected int dissatisfactionCounter = 0;
	protected long currentDiscussionStartTime = -1; // current Time
	
	@Override
	void setTable(AID table) {
		this.table = table;
		//the moment I sit at a (new table) time starts counting
		//this.currentDiscussionStartTime = System.currentTimeMillis();
		   //removed again since just using an 0,1,2,3 is easier to handle
		   // than these damn variant milliseconds^^
		dissatisfactionCounter = 0;
	}
	
	@Override
	void updateDiscussionTopic(InterestSet topic) {
		super.updateDiscussionTopic(topic);
		//if(currentDiscussionStartTime == -1 || table == null)
		//	throw new Exception("I must be seated at a table to have a discussion");
		
		//long now = System.currentTimeMillis();
		long now = currentDiscussionStartTime + 1;
		
		BigDecimal duration = new BigDecimal(now - currentDiscussionStartTime);
		BigDecimal satisfaction = interests.addSatisfaction(topic, duration);
		
		this.currentDiscussionStartTime = now;
		if(interests.size() * 0.9 < interests.sum().doubleValue())
		{//I've discussed 90% of my interests.. I am soo happy and exhausted let's go
			System.out.println(this.getName() + " went home happily.");
			this.doDelete();
		}
		System.out.println(this.getName() + " gained " + satisfaction.floatValue() + " satisfaction from discussing.");
		
		if(satisfaction.doubleValue() < 0.1)
		{
			dissatisfactionCounter ++;
			if(dissatisfactionCounter > 3)
				//this table sucks
				addBehaviour(new DeregisterAtTableBehaviour(getAgent(), getAgent().getTable()));
			if(dissatisfactionCounter > 10)
			{	//this conference sucks
				System.out.println(this.getName() + " went home unhappy.");
				this.doDelete();
			}
		}	
		else
			dissatisfactionCounter = 0;

		currentDiscussionStartTime++;
	}

	private static final long serialVersionUID = 8892182120861989549L;

}
